﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace StockholmsStad.Windows.UI.Infrastructure.Controls
{
    /// <summary>
    /// Interaction logic for AndersTest.xaml
    /// </summary>
    public partial class CustomDatePicker : UserControl, INotifyPropertyChanged
    {
        static DependencyProperty DateValueProperty;

        public event PropertyChangedEventHandler PropertyChanged;

        static CustomDatePicker()
        {
            PropertyChangedCallback dateValueChangedCallback = new PropertyChangedCallback(DateValueChanged);
            PropertyMetadata metaData = new PropertyMetadata(dateValueChangedCallback);

            DateValueProperty = DependencyProperty.Register("DateValue", typeof(string), typeof(CustomDatePicker), metaData);
        }

        public CustomDatePicker()
        {
            InitializeComponent();
        }

        private static void DateValueChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
        {
            //tbText.Text = args.NewValue.ToString();
        }

        private void openPopup_Click(object sender, MouseButtonEventArgs e)
        {
            //RefreshDateValue();

            popupControl.IsOpen = true;
        }

        private void MonthCalendar_DateSelectionChanged(object sender, Microsoft.Samples.KMoore.WPFSamples.DateControls.DateSelectionChangedEventArgs e)
        {
            tbText.Text = e.AddedDates[0].ToString("yyyy-MM-dd");

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("DateValue"));

            popupControl.IsOpen = false;
        }

        public DateTime? DateValue
        {
            get { return dateControl.SelectedDate; }
            set 
            {
                dateControl.SelectedDate = value;

                tbText.Text = dateControl.SelectedDate.HasValue ? dateControl.SelectedDate.Value.ToString("yyyy-MM-dd") : String.Empty;
            }
        }

        private void tbText_LostFocus(object sender, RoutedEventArgs e)
        {
            //RefreshDateValue();
        }

        private void RefreshDateValue()
        {
            return;

            try
            {
                dateControl.SelectedDate = DateTime.Parse(tbText.Text);
                tbText.Text = dateControl.SelectedDate.HasValue ? dateControl.SelectedDate.Value.ToString("yyyy-MM-dd") : String.Empty;
            }
            catch 
            {
                dateControl.SelectedDate = DateTime.Now;
                tbText.Text = String.Empty;
            }
        }
    }
}
